import rosbag
from std_msgs.msg import UInt8MultiArray
from sensor_msgs.msg import Image
bag = rosbag.Bag('test.bag', 'w')

import rospy
# import ImageFile
from PIL import ImageFile
from PIL import Image as ImagePIL


a = UInt8MultiArray()
a.data = [1,2,3]
bag.write('array', a)

imgPath = '/home/yujr/rosbag-tool/saveBag/26-22-45-40/img/1550134147254.422363.png'
print("Adding %s" % imgPath)
fp = open(imgPath, "r")
p = ImageFile.Parser()

imgpil = ImagePIL.open(imgPath)
width, height = imgpil.size

while 1:
    s = fp.read(1024)
    if not s:
        break
    p.feed(s)

im = p.close()

imagetimestamps = 1550134147254.422363
Stamp = rospy.rostime.Time.from_sec(float(imagetimestamps))

'''set image information '''
Img = Image()

Img.header.stamp = Stamp
Img.height = height
Img.width = width
Img.header.frame_id = "camera"

'''for mono8'''
Img.encoding = "mono8"
Img_data = [pix for pixdata in [im.getdata()] for pix in pixdata]
Img.step = Img.width

Img.data = Img_data
bag.write('camera/image_raw', Img, Stamp)

bag.close()